/**
 * @(#)CodeEntityExtendRelation.java, 2008-4-30.
 * Copyright 2008  Program Analysis Research Group(PARG),Institute of Software, Peking University.
 * All rights reserved.
 */
package edu.pku.sei.pgie.analyzer.internal.relation;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.TypeDeclaration;

import edu.pku.sei.pgie.analyzer.internal.CodeEntitySymbolTable;

/**
 * @author HeLi
 */
public class CodeEntityExtendRelation extends AbstractCodeEntityRelation {

	class ExtendVisitor extends ASTVisitor {
		List<String[]> container;

		public ExtendVisitor() {
			this.container = new ArrayList<String[]>();
		}

		public boolean visit(TypeDeclaration node) {
			ITypeBinding typeBinding = node.resolveBinding();
			if (typeBinding == null) {
				return true;
			}
			ITypeBinding superTypeBinding = typeBinding.getSuperclass();
			if (superTypeBinding == null) {
				return true;
			}

			container.add(new String[] { typeBinding.getQualifiedName(),
					superTypeBinding.getQualifiedName() });
			return true;
		}

		public List<String[]> getResult() {
			return this.container;
		}
	}

	public String[][] collectCallBack(CompilationUnit root,
			CodeEntitySymbolTable symbolTable) throws Exception {
		ExtendVisitor visitor = new ExtendVisitor();
		root.accept(visitor);
		List<String[]> result = visitor.getResult();
		return result.toArray(new String[result.size()][]);
	}
}
